<?php
/**
 * DESC:
 * 作者:晨光
 * EMAIL:461718846@qq.com
 * DATE:2020-06-03
 */

namespace app\wechat\controller;

use app\common\controller\WechatUserBase;
use app\common\model\GoodsFormat;
use app\common\model\Order;
use think\Db;
use think\facade\Request;

class Goods extends WechatUserBase
{
//    获取商品列表
    public function getGoodsList(){
        $param = input();
        $lat = $param['latitude'];
        $lng = $param['longitude'];
        if (!empty($lat) && is_numeric($lat) && !empty($lng) && is_numeric($lng)){
            $address = getAddress($param['latitude'],$param['longitude']);
        }else{
            $addressByIp = getAddressBtIp();
            if (empty($addressByIp)){
                $this->error('获取位置失败，请打开手机定位');
            }
            $address['province'] = $addressByIp['ad_info']['province'];
            $address['city'] = $addressByIp['ad_info']['city'];
            $address['district'] = $addressByIp['ad_info']['district'];
            $lat = $addressByIp['location']['lat'];
            $lng = $addressByIp['location']['lng'];
        }

        if (empty($address)){
            $this->error('定位失败，请重新进入');
        }

        $where = [];

//        只能查询本市区的
        $where[] = ['province','=',$address['province']];
        $where[] = ['city','=',$address['city']];
        $where[] = ['area','=',$address['district']];
        if (!empty($param['keywords'])){
            $where[] = ['name','like','%'.$param['keywords'].'%'];
        }
//        只查看上架的
        $where[] = ['put_on','=',10];
//        获取所有营业的店铺id,只看营业店铺的商品
        $shop_id = \app\common\model\Shop::where('state',10)->column('id');
        $where[] = ['shop_id','in',$shop_id];
        $data = \app\common\model\Goods::with(['shop'])->where($where)
            ->field('*,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*('.$lat.'-lat)/360),2)+COS(PI()*'.$lat.'/180)* COS(lat * PI()/180)*POW(SIN(PI()*('.$lng.'-lng)/360),2)))) as distance')
            ->order('distance asc,top asc,id desc')->append(['original_price'])->paginate($param['limit'],false,['query'=>$param]);
        foreach ($data as $k=>&$v){
            $v['img_thumb'] = getFullPath($v['img_thumb']);
            $v['distance'] = number_format($v['distance'],2);
            if (empty($v['end_time'])){
                $v['end_time'] = '永久有效';
            }
        }
        $data = $data->toArray();
        return $this->result($data,$data['total'],'0',['address'=>$address]);
    }

//    获取商品详情
    public function getGoodsDetail(){
        $goods_id = input('goods_id');
        $data = \app\common\model\Goods::with(['shop'])->where('id','=',$goods_id)->find();
        if (!empty($data['carousel'])){
            $data['carousel'] = explode(',',$data['carousel']);
        }
        $data['sku'] = GoodsFormat::getVantSku($goods_id);
//        已经购买的数量
        $data['use_buy'] = Order::getUserBuy(session('wechat.id'));
        $data['img_thumb'] = getFullPath($data['img_thumb']);
        $data = $data->toArray();
        return $this->result($data);
    }

//    获取商品列表--商家
    public function getGoodsListByShop(){
        $param = input();
        $where = [];
        if (!empty($param['shop_id']) && is_numeric($param['shop_id'])){
            $where[] = ['shop_id','=',$param['shop_id']];
        }
        if (!empty($param['keywords'])){
            $where[] = ['name','like','%'.$param['keywords'].'%'];
        }
        $data = \app\common\model\Goods::where($where)->append(['original_price'])->paginate($param['limit'],false,['query'=>$param]);
        foreach ($data as $k=>&$v){
            if (empty($v['end_time'])){
                $v['end_time'] = '永久有效';
            }
        }
        $data = $data->toArray();
        $this->result($data['data'],$data['total']);
    }

    public function goods_edit_save(){
        $res = \app\common\model\Goods::goods_edit_save(input());
        if ($res){
            $this->success();
        }else{
            $this->error();
        }
    }
}
